package com.ruoyi.web.controller.jl;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.jl.domain.Game;
import com.ruoyi.jl.domain.GameTable;
import com.ruoyi.jl.domain.custome.CustomGameTable;
import com.ruoyi.jl.mapper.GameMapper;
import com.ruoyi.jl.service.IGameTableService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 游戏桌 控制层
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Controller
@RequestMapping("/jl/gameTable")
public class GameTableController extends BaseController {
    private String prefix = "jl/gameTable";
	
	private final IGameTableService gameTableService;

	@Autowired
	GameMapper gameMapper;

	@Autowired
	public GameTableController(IGameTableService gameTableService) {
		this.gameTableService = gameTableService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("jl:gameTable:view")
	@GetMapping
	public String gameTable(){
	    return prefix + "/gameTable";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		CustomGameTable gameTable = gameTableService.queryById(id);
		mmap.put("gameTable", gameTable);
		return prefix + "/edit";
	}
	
	/**
	 * 查询游戏桌列表
	 */
	@RequiresPermissions("jl:gameTable:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(CustomGameTable gameTable){
		startPage();
        List<CustomGameTable> list = gameTableService.tableList(gameTable);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存游戏桌
	 */
	@RequiresPermissions("jl:gameTable:add")
	@Log(title = "游戏桌", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(GameTable gameTable){
		gameTable.setCreateTime(new Date());
		Integer prizePool = gameTable.getPrizePool();
		Integer variance = gameTable.getPrizePoolVariance();
		// 计算随机数
        Random random = new Random();
        int i = random.nextInt(variance * 2 + 1);
		gameTable.setPrizePool(prizePool - variance + i);
		gameTableService.save(gameTable);
		return Result.success();
	}

	/**
	 * 修改保存游戏桌
	 */
	@RequiresPermissions("jl:gameTable:edit")
	@Log(title = "游戏桌", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(GameTable gameTable){
        Integer prizePool = gameTable.getPrizePool();
        Integer variance = gameTable.getPrizePoolVariance();
        // 计算随机数
        Random random = new Random();
        int i = random.nextInt(variance * 2 + 1);
        gameTable.setPrizePool(prizePool - variance + i);
		gameTableService.updateById(gameTable);
		return Result.success();
	}
	
	/**
	 * 删除游戏桌
	 */
	@RequiresPermissions("jl:gameTable:remove")
	@Log(title = "游戏桌", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		gameTableService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出游戏桌列表
	 */
	@RequiresPermissions("jl:gameTable:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(GameTable gameTable){
		List<GameTable> list = gameTableService.list(gameTable);
		ExcelUtil<GameTable> util = new ExcelUtil<>(GameTable.class);
		util.exportExcel(list, "gameTable");
		return Result.success();
	}

	@Log(title = "修改状态", businessType = BusinessType.UPDATE)
	@RequiresPermissions("jl:player:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(GameTable table) {
		boolean flag = gameTableService.updateById(table);
		if(flag) {
			return success();
		} else {
			return error();
		}
	}
}
